package com.elinkway.infinitemovies.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.UmengEventPoster;

/**
 * Created by zhaopeipei on 2016/6/28.
 */
public class BaseFragment extends Fragment {

    private Context mContext;

    private String mClassName;

    protected LayoutInflater mLayoutInflater;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
        mLayoutInflater = activity.getLayoutInflater();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
        } else {
            if (!TextUtils.isEmpty(mClassName)) {
                postUmengVisitEvent();
                setCurrentClassName(mClassName);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(mClassName) && !isHidden()) {
            postUmengVisitEvent();
            setCurrentClassName(mClassName);
            LogUtils.i(mClassName, "onResume class name is " + mClassName + " " + isHidden());
        }
        Activity activity  = getActivity();
        if (activity instanceof NewMainActivity) {
            ((NewMainActivity) activity).isBackToForeGround = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!TextUtils.isEmpty(mClassName) && isVisibleToUser) {
            postUmengVisitEvent();
            setCurrentClassName(mClassName);
            LogUtils.i(mClassName, "class name is " + mClassName + " " + isVisibleToUser);
        }
    }

    public void setCurrentClassName(String childFragmentName) {
        MoviesApplication.getInstance().setLastPageClassName(childFragmentName);
    }

    public void setFragmentClassName(String className) {
        mClassName = className;
    }

    public void postUmengVisitEvent() {
        if (UtilsFragment.TAG.equals(mClassName)) {
            UmengEventPoster.postPageVisitTime(UmengEventPoster.VISIT_CHANNEL,
                    MoviesApplication.getInstance().getLastPageClassName());
        } else if (TopicFragment.TAG.equals(mClassName)) {
            UmengEventPoster.postPageVisitTime(UmengEventPoster.VISIT_TOPIC_LIST,
                    MoviesApplication.getInstance().getLastPageClassName());
        } else if (HomeTestFragment.TAG.equals(mClassName)) {
            UmengEventPoster.postPageVisitTime(UmengEventPoster.VISIT_RECOMMEND,
                    MoviesApplication.getInstance().getLastPageClassName());
        } else if (RankFragment.TAG.equals(mClassName)) {
            UmengEventPoster.postPageVisitTime(UmengEventPoster.VISIT_RANK,
                    MoviesApplication.getInstance().getLastPageClassName());
        }

    }

    public ActionBar getSupportActionBar() {
        Activity activity = getActivity();
        if (activity != null && activity instanceof AppCompatActivity) {
            return ((AppCompatActivity) activity).getSupportActionBar();
        }
        return null;
    }
}
